﻿
using System;
using System.Collections.Generic;
namespace gbL.NetBoard.Helpers
{
    /// <summary>
    /// Class which initialises objects which have declared contracts (implemented interfaces) which 
    /// enable them to participate in the blackboard system. 
    /// </summary>
    public class Builder
    {
        public Blackboard Blackboard { get; set; }

        public Builder(Blackboard blackboard)
        {
            Blackboard = blackboard;
        }

        public Builder() : this(new Blackboard()) { }

        public T Resolve<T>() where T : new() {
            T ret = new T();
            if (ret is IUseMessages)
            {
                Initialise((IUseMessages)ret);
            }
            return ret;
        }

        private void Initialise(IUseMessages adaptee)
        {
            adaptee.Messages = new MessagesAdapter(Blackboard);
            adaptee.ConfigureMessages();
        }
    }
}
